package com.linx.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.linx.common.core.domain.R;
import com.linx.common.core.validate.AddGroup;
import com.linx.common.core.validate.EditGroup;
import com.linx.common.excel.utils.ExcelUtil;
import com.linx.common.idempotent.annotation.RepeatSubmit;
import com.linx.common.log.annotation.Log;
import com.linx.common.log.enums.BusinessType;
import com.linx.common.mybatis.core.page.PageQuery;
import com.linx.common.mybatis.core.page.TableDataInfo;
import com.linx.common.web.core.BaseController;
import com.linx.system.domain.bo.AppRoleMenuBo;
import com.linx.system.domain.vo.AppRoleMenuVo;
import com.linx.system.service.IAppRoleMenuService;
import com.linx.system.service.ISysRoleService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * App角色和菜单关联
 *
 * @author linx
 * @date 2024-12-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/roleMenu")
public class AppRoleMenuController extends BaseController {

    private final IAppRoleMenuService appRoleMenuService;
    private final ISysRoleService roleService;
    /**
     * 查询App角色和菜单关联列表
     */
    @SaCheckPermission("system:roleMenu:list")
    @GetMapping("/list")
    public TableDataInfo<AppRoleMenuVo> list(AppRoleMenuBo bo, PageQuery pageQuery) {
        return appRoleMenuService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出App角色和菜单关联列表
     */
    @SaCheckPermission("system:roleMenu:export")
    @Log(title = "App角色和菜单关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppRoleMenuBo bo, HttpServletResponse response) {
        List<AppRoleMenuVo> list = appRoleMenuService.queryList(bo);
        ExcelUtil.exportExcel(list, "App角色和菜单关联", AppRoleMenuVo.class, response);
    }

    /**
     * 获取App角色和菜单关联详细信息
     *
     * @param roleId 主键
     */
    @SaCheckPermission("system:roleMenu:query")
    @GetMapping("/{roleId}")
    public R<AppRoleMenuVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long roleId) {
        return R.ok(appRoleMenuService.queryById(roleId));
    }

    /**
     * 新增App角色和菜单关联
     */
    @SaCheckPermission("system:roleMenu:add")
    @Log(title = "App角色和菜单关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppRoleMenuBo bo) {
        return toAjax(appRoleMenuService.insertByBo(bo));
    }

    /**
     * 修改App角色和菜单关联
     */
    @SaCheckPermission("system:roleMenu:edit")
    @Log(title = "App角色和菜单关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppRoleMenuBo bo) {
        return toAjax(appRoleMenuService.updateByBo(bo));
    }

    /**
     * 删除App角色和菜单关联
     *
     * @param roleIds 主键串
     */
    @SaCheckPermission("system:roleMenu:remove")
    @Log(title = "App角色和菜单关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] roleIds) {
        return toAjax(appRoleMenuService.deleteWithValidByIds(List.of(roleIds), true));
    }

    /**
     * 取消授权菜单
     */
    @SaCheckPermission("system:role:edit")
    @Log(title = "App角色和菜单", businessType = BusinessType.GRANT)
    @PutMapping("/cancel")
    public R<Void> cancelAuthAppRoleMenu(@RequestBody AppRoleMenuBo appRoleMenuBo) {
        return toAjax(appRoleMenuService.deleteAuthAppRoleMenu(appRoleMenuBo));
    }
    /**
     * 批量取消授权菜单
     *
     * @param roleId  角色ID
     * @param menuIds 菜单ID串
     */
    @SaCheckPermission("system:role:edit")
    @Log(title = "App角色和菜单", businessType = BusinessType.GRANT)
    @PutMapping("/cancelAll")
    public R<Void> cancelAuthppRoleMenuAll(Long roleId, Long[] menuIds) {
        return toAjax(appRoleMenuService.deleteAuthAppRoleMenus(roleId, menuIds));
    }


    /**
     * 查询已分配角色移动端菜单
     */
    @SaCheckPermission("system:role:list")
    @GetMapping("/allocatedList")
    public TableDataInfo<AppRoleMenuVo> allocatedList(AppRoleMenuBo bo, PageQuery pageQuery) {
        return appRoleMenuService.selectAllocatedList(bo, pageQuery);
    }    /**
     * 批量选择菜单授权
     *
     * @param roleId  角色ID
     * @param menuIds 菜单ID串
     */
    @SaCheckPermission("system:role:edit")
    @Log(title = "角色管理", businessType = BusinessType.GRANT)
    @PutMapping("/selectAll")
    public R<Void> selectAuthAppMenuAll(Long roleId, Long[] menuIds) {
        roleService.checkRoleDataScope(roleId);
        return toAjax(appRoleMenuService.insertAuthAppRoleMenus(roleId, menuIds));
    }

    /**
     * 查询未分配用户角色列表
     */
    @SaCheckPermission("system:role:list")
    @GetMapping("/unallocatedList")
    public TableDataInfo<AppRoleMenuVo> unallocatedList(AppRoleMenuBo bo, PageQuery pageQuery) {
        return appRoleMenuService.selectUnallocatedList(bo, pageQuery);
    }

}
